<template>
  <el-card>
    <el-row>
      <el-dropdown placement="bottom-start" @command="onCommand">
        <el-button> 请假状态 </el-button>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item command="all">全部</el-dropdown-item>
            <el-dropdown-item command="1">已同意</el-dropdown-item>
            <el-dropdown-item command="0">待审批</el-dropdown-item>
            <el-dropdown-item command="2">已驳回</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </el-row>
    <el-row>
      <el-table
        :data="aoorovalData"
        style="width: 100%; overflow: auto; height: 60vh"
      >
        <el-table-column label="学号" prop="studentId"></el-table-column>
        <el-table-column label="姓名" prop="student"></el-table-column>
        <el-table-column
          label="请假事由"
          prop="leaveReason"
          width="200"
        ></el-table-column>
        <el-table-column
          label="请假开始时间"
          prop="createTime"
        ></el-table-column>
        <el-table-column label="请假结束时间" prop="endTime"></el-table-column>
        <el-table-column
          label="总天数"
          prop="leaveDay"
          width="80"
        ></el-table-column>
        <el-table-column label="状态">
          <template #default="scope">
            <el-tag :type="statusTag(scope.row.state)">{{
              statusText(scope.row.state)
            }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button
              size="small"
              type="success"
              @click="handleagree(scope.row)"
              >同意</el-button
            >
            <el-button
              size="small"
              type="danger"
              @click="handlerefuse(scope.row)"
              >驳回</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-row>
    <!-- 分页器 -->
    <el-row>
      <el-col :span="12">
        <el-pagination
          style="margin-top: 15px"
          v-model:current-page="searchParams.pageNum"
          v-model:page-size="searchParams.pageSize"
          :page-sizes="[5, 10, 20]"
          background
          layout="total, sizes, prev, pager, next"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </el-col>
    </el-row>
  </el-card>
</template>

<script setup>
import { ref, reactive, onMounted, computed } from "vue";
import { getaooroval, arrgeaooroval, rejectaooroval } from "@/api/aooroval";
import { ElMessage } from "element-plus";

const aoorovalData = ref([]);
const currentStatus = ref(null);

// 分页器
const total = ref(0); // 总条数
const searchParams = reactive({
  pageNum: 1,
  pageSize: 5,
}); // 分页查询条件

const getaoorovalList = async () => {
  const res = await getaooroval(searchParams.pageNum, searchParams.pageSize);
  aoorovalData.value = res.data.rows;
  total.value = res.data.total;
};

const handleSizeChange = (newSize) => {
  searchParams.pageSize = newSize;
  searchParams.pageNum = 1; // 当改变页面大小时，重置为第一页
  getaoorovalList();
};

const handleCurrentChange = (newPage) => {
  searchParams.pageNum = newPage;
  getaoorovalList();
};
const statusTag = (state) => {
  switch (state) {
    case 0:
      return "info"; // 蓝色
    case 1:
      return "success"; // 绿色
    case 2:
      return "danger"; // 红色
    default:
      return "info";
  }
};

const statusText = (state) => {
  switch (state) {
    case 0:
      return "待审批";
    case 1:
      return "已通过";
    case 2:
      return "已驳回";
    default:
      return "未知状态";
  }
};

const handleagree = (row) => {
  arrgeaooroval({ id: row.id, state: row.state, studentId: row.studentId });
};

const handlerefuse = (row) => {
  rejectaooroval({ id: row.id, state: row.state, studentId: row.studentId });
};

const onCommand = (command) => {
  currentStatus.value = command === "all" ? null : parseInt(command);
  if (command === "all") {
    getaoorovalList();
  }
};

onMounted(() => {
  getaoorovalList();
});
</script>

<style scoped>
.status-pending {
  color: blue;
}

.status-approved {
  color: green;
}

.status-rejected {
  color: red;
}
</style>
